<?php

namespace App\Observers;
use App\Models\User;
use DB;
use App\Jobs\setAvatarToDoubanUser;
class UserObserver
{
    /**
     * 处理用户删除事件
     *
     * @param User $user
     * @return void
     */
    public function deleted(User $user)
    {
        // 删除用户设置
        $user->setting()->delete();
        // 删除用户看单
        $user->collects()->delete();
        // 删除用户关注
        $user->followings()->detach();
        // 删除所有粉丝
        DB::table('followers')->where('user_id', $user->id)->delete();
        // 删除打卡数据
        $user->movies()->detach();
        // 删除短评
        $user->comments()->delete();
    }

    /**
     * 处理用户创建事件
     *
     * @return void
     */
    public function created(User $user)
    {
        if ($user->type === 'douban') {
            dispatch(new setAvatarToDoubanUser($user));
        }
    }
}
